package com.bigger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bigger.constant.ErrorConstant;
import com.bigger.context.BaseContext;
import com.bigger.dto.LoginDTO;
import com.bigger.dto.UpdatePasswordDTO;
import com.bigger.entity.Employee;
import com.bigger.entity.Order;
import com.bigger.exception.LoginException;
import com.bigger.exception.PowerException;
import com.bigger.mapper.EmployeeMapper;
import com.bigger.mapper.OrderMapper;
import com.bigger.service.RunnerService;
import com.bigger.vo.WagesVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;

@Service
public class RunnerServiceImpl implements RunnerService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void updatePassword(UpdatePasswordDTO updatePasswordDTO) {
        if (!updatePasswordDTO.getNewPassword().matches("\\w{6,15}")){
            throw new LoginException(ErrorConstant.PASSWORD_FORMAT_ERROR);
        }
        Employee employee = employeeMapper.selectByUsername(updatePasswordDTO.getUsername());
        if(employee==null){
            throw new LoginException(ErrorConstant.USER_NOT_EXIST);
        }
        if (!employee.getId().equals(BaseContext.getCurrentId())){
            throw new PowerException(ErrorConstant.INSUFFICIENT_PRIVILEGES);
        }
        String password = employee.getPassword();
        String oldPassword = DigestUtils.md5DigestAsHex(updatePasswordDTO.getOldPassword().getBytes(StandardCharsets.UTF_8));
        if (!password.equals(oldPassword)){
            throw new LoginException(ErrorConstant.OLD_PASSWORD_ERROR);
        }
        String newPassword = DigestUtils.md5DigestAsHex(updatePasswordDTO.getNewPassword().getBytes(StandardCharsets.UTF_8));
        employee.setPassword(newPassword);
        employee.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        employeeMapper.updateById(employee);
    }

    @Override
    public WagesVO getWage() {
        LocalDate now = LocalDate.now(ZoneId.of("Asia/Shanghai"));
        LocalDateTime start=LocalDateTime.of(
                now.with(TemporalAdjusters.firstDayOfMonth()) //获取当月的月初日期
                , LocalTime.MIN
        );
        LocalDateTime end=LocalDateTime.of(
                now.with(TemporalAdjusters.lastDayOfMonth()) //获取当月的月末日期
                , LocalTime.MAX
        );
        WagesVO wages = employeeMapper.wages(start, end, Order.FINISH, BaseContext.getCurrentId());
        if (wages==null){
            Employee employee = getById();
            return new WagesVO(employee.getName(),employee.getPhone(),0,0.0); //表示本月无收入
        }
        BigDecimal totalWages=new BigDecimal(wages.getTotalWages().toString());
        totalWages=totalWages.multiply(new BigDecimal("0.7"));//进行3 7分
        wages.setTotalWages(totalWages.doubleValue());
        return wages;
    }

    @Override
    public Employee getById() {
        Employee employee = employeeMapper.selectById(BaseContext.getCurrentId());
        return employee;
    }
}
